<?php

/**
 * 
 */
class Report extends CI_Controller {

    var $count = 0;
    var $def = 0;
    var $thu = 0;
    var $comm = 0;
    var $oth_comm = 0;

    function __construct() {
        parent::__construct();
        $this->load->helper(array('html', 'url'));
        $this->load->model('reports_model');
        $this->load->library('form_validation');
        $this->load->library('googlemaps');
        $this->load->model('thumbs_model');
        $this->load->model('comments_model');
    }

    public function index($id = NULL) {

        if ($id <> NULL) {
            $data['fetch'] = $this->reports_model->fetchReportByCity($id);

            $data['no_comments'] = $data['fetch']['n_comments'];
            $data['source'] = $data['fetch']['source'];
            $data['area'] = $data['fetch']['name'];
            $data['lat'] = $data['fetch']['latitude'];
            $data['lon'] = $data['fetch']['longitude'];
            if ($data['fetch']['reported'] > 0) {
                $data['reported'] = '<div style="color:#cc6666;text-align:right;font-size:10px;padding-top: 2px;padding-right: 7px;">Udalosť nahlásená ako chybná</div>';
            } else {
                $data['reported'] = '<div style="color:gray;text-align:right;font-size:10px;padding-top: 2px;padding-right: 7px;cursor: pointer;" class="report_report" id=' . $data['fetch']['report_id'] . '>Nahlásiť udalosť ako chybnú</div>';
            }
            $data['street'] = $data['fetch']['street'];
            $data['city'] = $this->removeZipcode($data['fetch']['city']);
            $data['seen'] = $this->dateFormatToSlovak($data['fetch']['seen']);
            $data['seen_daysAgo'] = $this->daystoHuman($this->daysBetweenDates($this->toDate($data['fetch']['seen']), date('Y-m-d')));
            $data['lastFive'] = $this->reports_model->fetchLastReportOccurences($data['fetch']['city'], $data['fetch']['street'], $id);
            $pocet = $this->reports_model->fetchLastReportOccurencesCount($data['fetch']['city'], $data['fetch']['street'], $id);

            (int) $data['pocet_vs'] = $pocet;
            foreach ($data['lastFive'] as $key) {
                $plus = $this->def++;
                $data['daysBetween'][$plus] = $this->daystoHuman($this->daysBetweenDates($this->toDate($key['seen']), date('Y-m-d')));

                // $data['daysBetween'][$plus] = $this->daysBetweenDates($this->toDate($key['seen']),date('Y-m-d'));
                $data['seen_last5'][$plus] = $this->timeFormatToSlovak($key['seen']);
                $data['comments_last5'][$plus] = $key['n_comments'];
                $data['type_last5'][$plus] = $type_[$plus] = $this->typeOfReport($key['type_id']);
                $data['obr_last5'][$plus] = $type_[$plus] = $this->typeOfReport($key['type_id']);
            }

            $lol = $this->typeOfReport($data['fetch']['type_id']);
            $data['typ'] = $lol['popis'];
            $data['obr'] = $lol['obr'];

            $config['center'] = $data['lat'] . "," . $data['lon'];

            $config['map_height'] = 180;
            $config['sensor'] = false;
            $config['map_width'] = 198;
            $config['draggable'] = false;
            $config['disableMapTypeControl'] = TRUE;
            $config['disableStreetViewControl'] = TRUE;
            $config['zoom'] = '12';

            $this->googlemaps->initialize($config);

            $marker = array();
            $marker['position'] = $data['lat'] . "," . $data['lon'];
            // $marker['draggable'] = true;
            $marker['icon'] = base_url() . "css/images/" . $data['obr'];
            // $marker['infowindow_content'] = $data['typ'];

            $this->googlemaps->add_marker($marker);

            $data['map'] = $this->googlemaps->create_map();




            if ($this->thumbs_model->fetchThumbById($id) != null) {
                $data['thumbs_arr'] = $this->thumbs_model->fetchThumbById($id);

                foreach ($data['thumbs_arr'] as $key) {
                    $plus = $this->thu++;
                    $data['t_decision'][$plus] = $key['decision']; // 
                    $data['t_count'][$plus] = $key['count'];
                }

                if (count($data['thumbs_arr']) == 1) {

                    if ($data['thumbs_arr'][0]['decision'] == 'up') {
                        $up_count = $data['thumbs_arr'][0]['count'];
                        $down_count = 0;
                    } else {
                        $down_count = $data['thumbs_arr'][0]['count'];
                        $up_count = 0;
                    }
                } else {
                    $up_count = $data['thumbs_arr'][0]['count'];
                    $down_count = $data['thumbs_arr'][1]['count'];
                }

                $num_of_thumbs = $up_count + $down_count;
                $data['tups'] = $up_count;
                $data['dups'] = $down_count;
                $data['percent_tup'] = floor(($up_count / $num_of_thumbs) * 100);
                $data['percent_dup'] = floor(($down_count / $num_of_thumbs) * 100);
            } else {
                $data['tups'] = 0;
                $data['dups'] = 0;
                $data['percent_tup'] = 0;
                $data['percent_dup'] = 0;
            }

            if ($this->session->userdata("sess_user_id")) {
                $user_id = $this->session->userdata("sess_user_id");
                if ($this->thumbs_model->hasVoted($user_id, $id)) {
                    $arr = $this->thumbs_model->hasVoted($user_id, $id);
                    $data['decision'] = $arr['decision'];
                }
            }

            $data['comment'] = $this->comments_model->fetchComments($id);
            foreach ($data['comment'] as $key) {
                $plus = $this->comm++;
                $data['comment_title'][$plus] = ucfirst($key['title']);
                $data['comment_author'][$plus] = $key['login'];
                $data['comment_id'][$plus] = $key['comment_id']; //
                if ($key['reported'] > 0) {
                    $data['comment_reported'][$plus] = '<span style="color:#cc6666;">Nahlásené</span>';
                    $data['comment_text'][$plus] = "<span class='batn' style='color: #BBBBBB;'>Komentár bol nahlásený ako nevhodný. Zobraziť komentár</span><span class='zobraz_' style='display:none;'>" . $key['comment_text'] . "</span>";
                } else {
                    $data['comment_reported'][$plus] = '<span class="report_comment" style="cursor: pointer;" id=' . $key['comment_id'] . '>Nahlásiť</span>';
                    $data['comment_text'][$plus] = $key['comment_text'];
                }

                $data['comment_date'][$plus] = $this->dateFormatToSlovak($key['added']);
            }
            if ($this->comments_model->fetchCommentsByPlace($data['fetch']['city'], $data['fetch']['street'], $id) != null) {

                /* ostatne komentare start */
                $data['other_comments'] = $this->comments_model->fetchCommentsByPlace($data['fetch']['city'], $data['fetch']['street'], $id);

                foreach ($data['other_comments'] as $key) {
                    $plus = $this->oth_comm++;
                    $typ = $this->typeOfReport($key['type_id']);
                    $data['other_comment_type_id'][$plus] = $typ['popis'];
                    $data['other_comment_pic'][$plus] = $typ['obr'];
                    $data['other_comment_id'] = $key['comment_id']; //
                    $data['other_comment_title'][$plus] = ucfirst($key['title']);
                    $data['other_comment_author'][$plus] = $key['login'];
                    $data['other_comment_text'][$plus] = nl2br($key['comment_text']);
                    $data['other_comment_date'][$plus] = $this->dateFormatToSlovak($key['added']);
                }

                /* ostatne komentare koniec */
            }

            $this->load->view('templates/header', $data);
            $this->load->view('reportInfo');
            $this->load->view('templates/footer');

// * cast komentov
            // koniec casti komentov
        }
    }

    public function typeOfReport($type) {
        switch ($type) {
            case '1':
                $popis = "Radar";
                $obr = "speed.png";
                break;
            case '2':
                $popis = "Policajná hliadka";
                $obr = "police.png";
                break;
            case '3':
                $popis = "Nehoda";
                $obr = "accident.png";
                break;
            case '4':
                $popis = "Mobilný radar";
                $obr = "mobile_radar.png";
                break;
        }
        return array('popis' => $popis, 'obr' => $obr);
    }

    public function removeZipcode($city) {
        return ltrim(preg_replace("/[0-9]/", "", $city));
    }

    public function dateFormatToSlovak($date) {
        return date('d.m.Y H:i', strtotime($date));
    }

    public function timeFormatToSlovak($time) {
        return date('H:i', strtotime($time));
    }

    public function toDate($date) {
        return date('Y-m-d', strtotime($date));
    }

    public function toTime($time) {
        return date('H:i:s', strtotime($time));
    }

    public function daysBetweenDates($date1, $date2) {
        $date1 = strtotime($date1);
        $date2 = strtotime($date2);
        return $days_between = ceil(abs($date1 - $date2) / 86400);
    }

    public function daystoHuman($days) {
        $day = "";
        switch ((int) $days) {
            case '0':
                $day = "Dnes";
                break;
            case '1':
                $day = "Včera";
                break;
            case '2':
                $day = "Predvčerom";
                break;
            default:
                $day = "Pred " . $days . " dňami";
                break;
        }
        return $day;
    }

}

?>